package com.train.classeval.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.train.classeval.entity.EvalActivity;
import com.train.classeval.entity.ProRatLoginRequest;
import com.train.classeval.entity.ProfessorEval;
import com.train.classeval.entity.ProfessorRating;
import com.train.classeval.service.impl.EvalActivityServiceImpl;
import com.train.classeval.service.impl.ProfessorEvalServiceImpl;
import com.train.classeval.service.impl.ProfessorRatingServiceImpl;
import com.train.classeval.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2021-10-23
 */
@RestController
public class ProfessorRatingController {

    @Autowired
    private ProfessorRatingServiceImpl professorRatingService;

    @Autowired
    private ProfessorEvalServiceImpl professorEvalService;

    @Autowired
    private EvalActivityServiceImpl evalActivityService;
    /**
     * 添加评价的明细
     * @param professorRatingList
     * @param eid
     * @return
     */
    @PostMapping("/addRating/{eid}/{pid}")
    public JsonData addRating(@RequestBody List<ProfessorRating> professorRatingList,
                              @PathVariable("eid") Integer eid,
                              @PathVariable("pid") Integer pid) {
        //遍历要评价的
        for (ProfessorRating professorRating : professorRatingList) {
//           打分明细表基本信息，统一权重0.1
            Integer weight = 1;
            professorRating.setCreateDate(LocalDateTime.now());
            professorRating.setUpdateDate(LocalDateTime.now());
            professorRating.setWeight(weight);
            Integer score = professorRating.getScore();
            Integer newScore = score * weight;
            //实际情况称上权重
            professorRating.setScore(newScore);
            professorRating.setOther("暂无说明，继续加油");
            //活动的id
            professorRating.setEvalId(eid);
            professorRating.setPRStatus("1");
            professorRatingService.save(professorRating);
        }
        //根据活动id和专家id找到
        QueryWrapper<ProfessorEval> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("professor_id",pid);
        queryWrapper.eq("activity_id", eid);
        //提交以后应该修改专家评价表评价状态，置为0(已评价)
        //查出具体的某个专家对某个活动的评价，改变其评价状态
        final ProfessorEval one = professorEvalService.getOne(queryWrapper);
        one.setPEStatus(0);
        professorEvalService.update(one, queryWrapper);
        //检查当前活动专家是否已经写完
        final Integer count = evalActivityService.sumPES(eid);
        //如果状态和值不为0则说明没有评价完就不管
        if (count != 0) {
            return JsonData.BuildSuccess("评价成功!");
        }
        if (count == 0) {
            //更新活动状态
            QueryWrapper<EvalActivity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("id",eid);
            EvalActivity byId = evalActivityService.getById(eid);
            byId.setEAStatus(0);
            evalActivityService.update(byId, queryWrapper1);
            return JsonData.BuildSuccess("评价成功!");
        }
        return JsonData.BuildSuccess("评价成功!");
    }
}
